package com.xiaobao.campusLostFoundAfter.controller;

import com.github.pagehelper.PageInfo;
import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.pojo.BrowsingHistory;
import com.xiaobao.campusLostFoundAfter.service.BrowsingHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/*浏览记录*/
@RestController
@Slf4j
@RequestMapping("/browsingHistory")
public class BrowsingHistoryController {
    private final BrowsingHistoryService browsingHistoryService;

    public BrowsingHistoryController(BrowsingHistoryService browsingHistoryService) {
        this.browsingHistoryService = browsingHistoryService;
    }

    //添加浏览记录
    @PostMapping("/add")
    public Result<String> addBrowsingHistory(@RequestBody BrowsingHistory browsingHistory) {
        log.info("添加浏览记录: {}", browsingHistory);
        return browsingHistoryService.addBrowsingHistory(browsingHistory);
    }

    //删除浏览记录
    @DeleteMapping("/delete/{bhId}")
    public Result<String> deleteBrowsingHistory(@PathVariable Integer bhId) {
        return browsingHistoryService.deleteBrowsingHistory(bhId);
    }

    //分页获取浏览记录
    @GetMapping("/page/found")
    public Result<PageInfo<BrowsingHistory>> getBrowsingHistory(@RequestParam(defaultValue = "") String userId,
                                                                @RequestParam(defaultValue = "1") int page,
                                                                @RequestParam(defaultValue = "10") int size) {
        PageInfo<BrowsingHistory> pageInfo = browsingHistoryService.getBrowsingHistoryOfFound(userId, page, size);
        return Result.success(pageInfo);
    }

    @GetMapping("/page/lost")
    public Result<PageInfo<BrowsingHistory>> getBrowsingHistoryOfLost(@RequestParam(defaultValue = "") String userId,
                                                                      @RequestParam(defaultValue = "1") int page,
                                                                      @RequestParam(defaultValue = "10") int size) {
        PageInfo<BrowsingHistory> pageInfo = browsingHistoryService.getBrowsingHistoryOfLost(userId, page, size);
        return Result.success(pageInfo);
    }

    //更新浏览记录的时间
    //根据浏览记录的id  更新浏览记录
    @PutMapping("/update")
    public Result<String> updateBrowsingHistory(@RequestParam Integer bhId) {
        return browsingHistoryService.updateBrowsingHistory(bhId);
    }

}
